import request from '@/utils/request'
import CustomForm from './Form'
import { mapGetters } from 'vuex'
import LogForm from './Log'
import BatchIntegral from './batchIntegral/index.vue'
import { deepClone } from '@/utils'
import ImportForm from './ImportForm'
import ExportBox from './ExportBox'
export default {
  components: { CustomForm, LogForm, BatchIntegral, ImportForm, ExportBox },
  data() {
    return {
      query: {
        provinceId: undefined,
        info: undefined,
        brand: 'bell'
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'asc',
        sidx: 'ranking'
      },
      formVisible: false,
      logVisible: false,
      exportBoxVisible: false,
      importFormVisible: false,
      columnList: [{ prop: 'brandText', label: '校区品牌' }, { prop: 'name', label: '校区简称' }, { prop: 'province', label: '省份' }, { prop: 'campusType', label: '校区类型' }, { prop: 'levelText', label: '校区阶段' }, { prop: 'campusStatus', label: '校区状态' }, { prop: 'points', label: '积分值' }, { prop: 'provinceRanking', label: '省排名' }, { prop: 'ranking', label: '全国排名' }, { prop: 'updateTime', label: '最近更新时间' }],
      typeOptions: [{ fullName: '人工操作', id: 'Artificial' }, { fullName: '系统操作', id: 'System' }],
      campusTypeOptions: [],
      brandOptions: [],
      batchVisible: false
    }
  },
  computed: {
    ...mapGetters(['handleSelection'])
  },
  created() {
    this.$store.dispatch('integral/handleSelectionClear')
    this.initData()
    this.$store.dispatch('base/getDictionaryData', { sort: 'CampusType' }).then(res => {
      this.campusTypeOptions = res
    })
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  methods: {
    handleSelectionChange(rows) {
      this.$store.dispatch('integral/handleSelection', {
        rows: rows,
        list: this.list
      })
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/campus/integral`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
        let rows = []
        if (this.handleSelection.length > 0) {
          this.list.map(item => {
            if (this.handleSelection.some(handle => handle.code === item.code)) {
              rows.push(item)
            }
          })
          this.$nextTick(() => {
            if (rows) {
              rows.forEach(row => {
                this.$refs['checked-table'].$refs['CustomTable'].toggleRowSelection(row, true)
              })
            }
          })
        }
      })
    },
    ImportFormHandle() {
      this.importFormVisible = true
      this.$nextTick(() => {
        this.$refs.ImportForm.init()
      })
    },
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(id, isDetail)
      })
    },
    addBatchOrUpdateHandle() {
      if (this.handleSelection.length < 1) {
        this.$message.error('请先选择校区')
      } else {
        this.batchVisible = true
        this.$nextTick(() => {
          this.$refs.batchForm.init()
        })
      }
    },
    logHandle(id) {
      this.logVisible = true
      this.$nextTick(() => {
        this.$refs.LogForm.init(id)
      })
    },
    download(data) {
      this.$message({
        message: '文件下载链接稍后推送到站内信，请在右上角铃铛图标处查收！',
        type: 'success',
        showClose: true,
        duration: 0
      })
      let _query = { ...data, ...this.listQuery, ...this.query }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      this.$refs.ExportBox.visible = false
      this.exportBoxVisible = false
      request({
        url: `/api/campus/integral/Actions/Export`,
        method: 'GET',
        data: query
      }).then(res => {
        this.$message({
          message: '数据导出成功，请查收！',
          type: 'success'
        })
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'asc',
        sidx: 'ranking'
      }
      this.initData()
    },

    sortChange({ column, prop, order }) {
      this.listQuery.ranking = order == 'ascending' ? 'asc' : 'desc'
      this.listQuery.sidx = !order ? '' : prop
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      this.batchVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      this.query = {
        provinceId: undefined,
        info: undefined,
        brand: 'bell'
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'asc',
        sidx: 'ranking'
      }
      this.initData()
    }
  }
}
